package com.feicheng.house.service.impl;

import com.feicheng.house.entity.Menjin;
import com.feicheng.house.entity.Shop;
import com.feicheng.house.entity.ShopMenjin;
import com.feicheng.house.entity.ShopMenjinVo;
import com.feicheng.house.mapper.ShopMenjinVoMapper;
import com.feicheng.house.result.ResultInfo;
import com.feicheng.house.service.IMenjinService;
import com.feicheng.house.service.IShopMenjinService;
import com.feicheng.house.service.IShopMenjinVoService;
import com.feicheng.house.service.IShopService;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author feichengTech
 * @since 2021-11-06
 */
@Service
public class ShopMenjinVoServiceImpl extends ServiceImpl<ShopMenjinVoMapper, ShopMenjinVo> implements IShopMenjinVoService {
	@Autowired
	IShopMenjinVoService iShopMenjinVoService;
	@Autowired
	IShopMenjinService iShopMenjinService;
	@Autowired
	IMenjinService iMenjinService;
	@Autowired 
	IShopService iShopService;
	
	/**
	 * see.IShopMenjinVoService.selectSMVoInfo
	 */
	@Override
	 public ResultInfo get(Integer menjinId) {
	  QueryWrapper<ShopMenjinVo> queryWrapper = new QueryWrapper<ShopMenjinVo>();
	  
	  queryWrapper.eq("menjin_id",menjinId);
	  return ResultInfo.success(iShopMenjinVoService.getOne(queryWrapper));
	 }
	/**
	 * see.IMenjinVoService.selectInfo
	 */
	 @Override
	 public ResultInfo selectInfo(String searchKey, Integer pageSize, Integer currentPage) {
	   QueryWrapper<ShopMenjinVo> queryWrapper = new QueryWrapper<ShopMenjinVo>();
	    //根据searchKey是否为空进行关键字查询
	    //判断searchKey是否为空，根据设备id和门店名称进行模糊查询
	    if (StringUtils.hasLength(searchKey)) {
	     queryWrapper.and(Wrapper -> Wrapper.like("UID", searchKey))
	             .or().like("shop_name", searchKey);
	    }
	    //分页查询
	    IPage<ShopMenjinVo> pageInfo = new Page<ShopMenjinVo>(currentPage,pageSize);
	    return ResultInfo.success(this.page(pageInfo,queryWrapper));
	 }
	/**
	 * see.IMenjinVoService.add
	 */
	@Override
	 public ResultInfo add(Menjin menjin) {
	  return ResultInfo.success(iMenjinService.save(menjin));
	 }
	/**
	 * see.IMenjinVoService.change
	 */
	@Override
	public ResultInfo change(ShopMenjinVo shopMenjinVo) {
		//构建条件状态
		QueryWrapper<ShopMenjin> queryWrapper = new QueryWrapper<ShopMenjin>();
		//条件
		queryWrapper.eq("menjin_id", shopMenjinVo.getMenjinId());
		//找到中间表的数据并删除
		if (iShopMenjinService.getOne(queryWrapper) != null) {
			iShopMenjinService.remove(queryWrapper);
		}
		//增加数据到中间表和基本表
		ShopMenjin shopMenjin = new ShopMenjin();
		shopMenjin.setMenjinId(shopMenjinVo.getMenjinId());
		shopMenjin.setShopId(shopMenjinVo.getShopId());
		iShopMenjinService.save(shopMenjin);
		Menjin menjin = new Menjin();
		menjin.setMenjinId(shopMenjinVo.getMenjinId());
		menjin.setTel(shopMenjinVo.getTel());
		menjin.setMenjinxinghao(shopMenjinVo.getMenjinxinghao());
		menjin.setUid(shopMenjinVo.getUid());
		menjin.setCodeval(shopMenjinVo.getCodeval());
		menjin.setCodetype(shopMenjinVo.getCodetype());
		menjin.setViewid(shopMenjinVo.getViewid());
		menjin.setUkey(shopMenjinVo.getUkey());
		menjin.setIsonline(shopMenjinVo.getIsonline());
		menjin.setProperty(shopMenjinVo.getProperty());
		menjin.setSn(shopMenjinVo.getSn());
		menjin.setBrushtime(shopMenjinVo.getBrushtime());
		iMenjinService.updateById(menjin); 
		return ResultInfo.success();
	}
	/**
	 * see.IMenjinVoService.del
	 */
	@Override
	 public ResultInfo del(Integer menjinId) {
	  // TODO Auto-generated method stub
		//构造条件状态
	  QueryWrapper<Menjin> queryWrapper = new QueryWrapper<Menjin>();
	    queryWrapper.eq("menjin_id", menjinId);
	    Menjin menjin = new Menjin();
	    //假删除
	    menjin.setDeleteFlag(false);
	    iMenjinService.update(menjin,queryWrapper);
	    //构造条件状态
	    QueryWrapper<ShopMenjin> queryWrapper1 = new QueryWrapper<ShopMenjin>();
	    //条件
	    queryWrapper1.eq("menjin_id", menjinId);
	    //删除中间表
	    if (iShopMenjinService.getOne(queryWrapper1) != null) {
	     iShopMenjinService.remove(queryWrapper1);
	    }
	  return ResultInfo.success();
	 }
	@Override
	public byte[] getQRCodeImage(String text, int width, int height) throws WriterException, IOException {
		// TODO Auto-generated method stub
		QRCodeWriter qrCodeWriter = new QRCodeWriter();
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
        
        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "JPG", pngOutputStream);
        byte[] pngData = pngOutputStream.toByteArray(); 
        return pngData;
	}

	
	


}
